package com.licode.codekit;

import com.intellij.ide.IdeView;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.licode.codekit.buildprj.GenerateCodeUI;
import com.licode.codekit.db.AssembledDb;
import com.licode.codekit.db.EntityBuilder;
import com.licode.codekit.db.GenerateCtx;
import com.licode.codekit.db.LiSysConfig;
import com.licode.codekit.utils.DbDriverDlUtil;
import com.licode.codekit.utils.PluginUtil;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;

/**
 * @author licode
 */
public class BuildCodeKit extends AnAction {

    @Override
    public void update(@NotNull AnActionEvent e) {
        ActionEventUtil.handleEvent(e);
        super.update(e);
    }

    @Override
    public void actionPerformed(AnActionEvent e) {
        IdeView ideView = e.getRequiredData(LangDataKeys.IDE_VIEW);
        //选择的文件夹
        PsiDirectory psiDirectory = ideView.getOrChooseDirectory();
        assert psiDirectory != null;
        //选择的项目
        Project project = psiDirectory.getProject();
        String currPrj = psiDirectory.getVirtualFile().getPath();

        GenerateCodeUI autoCodeUI = new GenerateCodeUI(project, currPrj);
        autoCodeUI.setResizable(true);
        PluginUtil.getPluginDir();
        handleMysqlDriver();

        autoCodeUI.show();

        // https://downloads.mysql.com/archives/get/p/3/file/mysql-connector-java-8.0.19.zip
    }

    private void handleMysqlDriver() {
        if (!AssembledDb.dbDriverValid()) {
            String driverPath = LiSysConfig.getInstance().getDbDriver();
            AssembledDb.initDbClassloader(driverPath, false);
            if (!AssembledDb.dbDriverValid()) {
                DbDriverDlUtil.downMysqlFromResource(driverPath, false);
                AssembledDb.initDbClassloader(driverPath, false);
            }
        }
    }

    @Test
    public void testDb() {
        String url = "jdbc:mysql://10.0.14.62:3306/wave_apartment?useUnicode=true&characterEncoding=utf8";
        AssembledDb db;
        String dbUser = "apartment";
        String dbPwd = "sRbzL1rO3OEHqS^I";
        String driverPath = "F:/data/mysql-connector-java-8.0.19.jar";
        EntityBuilder entityBuilder = new EntityBuilder();

        try {
            AssembledDb.initDbClassloader(driverPath, false);
            db = new AssembledDb();
            db.initTables(url, dbUser, dbPwd);
            if (db.getTables() != null && !db.getTables().isEmpty()) {
                String dir = "C:/Users/Administrator/Documents/test02/dddds/dddds-service/src/main/java/com/licode/wf/dddds";
                GenerateCtx gCtx = new GenerateCtx(dir, "PO.java");
                entityBuilder.generate(db.getTables().get(10), gCtx);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testDownloadMysqlDriver() {
        DbDriverDlUtil.downMysql8019Driver("c:/data/");
    }
}
